package cn.edu.neusoft.yuejian12.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian12.JavaBean.Login;
import cn.edu.neusoft.yuejian12.Model.UserModel;
import cn.edu.neusoft.yuejian12.JavaBean.Reg;
import cn.edu.neusoft.yuejian12.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    private ImageView btn_back;
    private Button btn_reg;
    private EditText account;
    private EditText password;
    private EditText password2;

    String str_tel,str_password,str_again;
    private String error;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvents();
        initData();
    }
    public void initViews()
    {
        btn_back=(ImageView)findViewById(R.id.btn_back);
        btn_reg=(Button)findViewById(R.id.reg_bt_reg);
        account=(EditText)findViewById(R.id.reg_et_account);
        password=(EditText)findViewById(R.id.reg_et_pass);
        password2=(EditText)findViewById(R.id.pass1);



    }
    public  void initEvents()
    {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(RegisterActivity.this,LoginActivity.class);
                startActivityForResult(intent,1);
            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str_tel=account.getText().toString();

                str_password=password.getText().toString();
                str_again=password2.getText().toString();
                UserModel userModel=new UserModel();
                Call call=userModel.reg(str_tel,str_password,str_again);
                call.enqueue(new Callback<Reg>(){
                    @Override
                    public void onResponse(Call<Reg> call, Response<Reg> response) {

                        error= response.body().getError();
                        if(!str_password.equals(str_again)){
                            Toast.makeText(getApplication(),"两次密码不同", Toast.LENGTH_SHORT).show();
                        }
                        else if (!"手机号已被使用".equals(error))
                        {
                            String str_tel=account.getText().toString();
                            Intent intent= new Intent();
                            intent.putExtra("username",str_tel);
                            setResult(RESULT_OK,intent);
                            Toast.makeText(getApplication(),"注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        else
                        {
                            Toast.makeText(getApplication(),"手机号已被使用", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Reg> call, Throwable t) {
                        Toast.makeText(getApplication(),"连接失败", Toast.LENGTH_SHORT).show();

                    }
                });


            }
        });
    }
    public  void initData()
    {

    }

}
